/**
 * Copyright &copy; 2012-2016 <a href="https://github.com/thinkgem/jeesite">JeeSite</a> All rights reserved.
 */
package com.thinkgem.jeesite.modules.hk.sys.service.user;

import com.google.common.collect.Lists;
import com.thinkgem.jeesite.common.persistence.Page;
import com.thinkgem.jeesite.common.service.CrudService;
import com.thinkgem.jeesite.common.utils.DateUtils;
import com.thinkgem.jeesite.common.utils.RandomValue;
import com.thinkgem.jeesite.modules.hk.media.entity.HkSystemFreight;
import com.thinkgem.jeesite.modules.hk.media.service.HkSystemFreightService;
import com.thinkgem.jeesite.modules.hk.shop.entity.shop.HkUserShop;
import com.thinkgem.jeesite.modules.hk.shop.service.shop.HkUserShopService;
import com.thinkgem.jeesite.modules.hk.sys.dao.user.HkUserDao;
import com.thinkgem.jeesite.modules.hk.sys.entity.user.HkUser;
import com.thinkgem.jeesite.modules.hk.sys.entity.user.HkUserProductFreight;
import com.thinkgem.jeesite.modules.sys.dao.UserDao;
import com.thinkgem.jeesite.modules.sys.entity.Office;
import com.thinkgem.jeesite.modules.sys.entity.Role;
import com.thinkgem.jeesite.modules.sys.entity.User;
import com.thinkgem.jeesite.modules.sys.service.SystemService;
import com.thinkgem.jeesite.modules.sys.utils.UserUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

import static com.thinkgem.jeesite.modules.sys.service.SystemService.entryptPassword;

/**
 * 用户管理Service
 *
 * @author 罗天文
 * @version 2018-06-15
 */
@Service
@Transactional(readOnly = true)
public class HkUserService extends CrudService<HkUserDao, HkUser> {

    @Autowired
    private HkUserShopService hkUserShopService;
    @Autowired
    private UserDao userDao;
    @Autowired
    private HkUserProductFreightService hkUserProductFreightService;
    @Autowired
    private HkSystemFreightService hkSystemFreightService;
    @Autowired
    private SystemService systemService;
    @Autowired
    private HkUserDao hkUserDao;

    public HkUser get(String id) {
        return super.get(id);
    }

    public List<HkUser> findList(HkUser hkUser) {
        return super.findList(hkUser);
    }

    public Page<HkUser> findPage(Page<HkUser> page, HkUser hkUser) {
        return super.findPage(page, hkUser);
    }

    @Transactional(readOnly = false)
    public void save(HkUser hkUser) {
        super.save(hkUser);
    }

    @Transactional(readOnly = false)
    public void delete(HkUser hkUser) {
        super.delete(hkUser);
    }

    @Transactional(readOnly = false)
    public void register(HkUser hkUser) {

        hkUser.setName(RandomValue.getCharAndNumr(6));
        hkUser.setNickname(RandomValue.getChineseName());
        hkUser.setCreateDate(DateUtils.parseDate(DateUtils.getDateTime()));
        hkUser.setUpdateDate(DateUtils.parseDate(DateUtils.getDateTime()));
        hkUser.setSex("1");
        hkUser.setAge("18");
        hkUser.setState("1");
        hkUser.setIntegral("0");
        hkUser.setType("1");
        hkUser.setHeadImg("/userfiles/1/images/sys/hkUser/2018/08/1.png");
        hkUser.setOccupation("职业");
        hkUser.setHobby("工作");
        this.save(hkUser);
        HkUserShop hkUserShop = new HkUserShop();
        String phone = hkUser.getMobile();
        User user = new User();
        user.setPassword(entryptPassword(phone.substring(phone.length() - 6, phone.length())));
        user.setLoginName(phone);
        user.setCompany(new Office("1"));
        user.setOffice(new Office("2"));
        user.setName(phone);
        user.setCreateBy(user);
        user.setUpdateBy(user);
        List<Role> roleList = Lists.newArrayList();
        Role r = new Role();
        r.setId("d650cc9f41c54e16be8b01af1bed4e7b");
        roleList.add(r);
        user.setRoleList(roleList);
        // 保存用户信息
        systemService.saveUser(user);
        hkUserShop.setUser(hkUser);
        hkUserShop.setManage(user);
        hkUserShopService.save(hkUserShop);

        //获取系统运费模板
        //向运费模板插入系统运费模板数据
        List<HkSystemFreight> listHkSys = hkSystemFreightService.findList(new HkSystemFreight());
        for (HkSystemFreight hk : listHkSys) {
            HkUserProductFreight hkUserProductFreight = new HkUserProductFreight();
            hkUserProductFreight.setShop(hkUserShop);
            hkUserProductFreight.setAddMoney(hk.getAddMoney());
            hkUserProductFreight.setAddPiece(hk.getPiece());
            hkUserProductFreight.setMoney(hk.getMoney());
            hkUserProductFreight.setIssystem("1");
            hkUserProductFreight.setName(hk.getName());
            hkUserProductFreight.setProvinceId(hk.getProvinceId());
            hkUserProductFreight.setUser(hkUser);
            hkUserProductFreight.setIsexcept(hk.getIsexcept());
            hkUserProductFreight.setPiece(hk.getPiece());
            hkUserProductFreightService.save(hkUserProductFreight);
        }
    }

    @Transactional(readOnly = false)
    public void updatePwdByName(String id, String userName, String passoword) {
        User user = new User(id);
        user.setPassword(entryptPassword(passoword));
        userDao.updatePasswordById(user);
        dao.updatePwdByMobile(userName, entryptPassword(passoword));
        // 清除用户缓存
        user.setLoginName(userName);
        UserUtils.clearCache(user);
    }

    @Transactional(readOnly = false)
    public void updatePasswordByName(String userName, String passoword) {
        String password = entryptPassword(passoword);
        userDao.updatePasswordByName(userName, password);
        dao.updatePwdByMobile(userName, password);
        // 清除用户缓存
        User user = new User();
        user.setLoginName(userName);
        UserUtils.clearCache(user);
    }

    @Transactional(readOnly = false)
    public void addIntegral(String manageId, Double integral) {
        dao.addIntegral(manageId, integral);
    }

}